﻿@model SW.Lite.Model.Master.RoleModel
@{
    ViewBag.Title = "权限管理";
    Layout = "~/Areas/Master/Views/Shared/_Layout.cshtml";
}
@section Header{
    <link href="~/Areas/Master/Common/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" />
    <script type="text/javascript" src="~/Areas/Master/Common/js/ztree/jquery.ztree.core-3.5.min.js"></script>
    <script type="text/javascript" src="~/Areas/Master/Common/js/ztree/jquery.ztree.excheck-3.5.min.js"></script>
    <script type="text/javascript">
        var nodes = @Html.Raw(ViewBag.Tree);
        var roleID = '@Model.Id';
        var objTree = null;
        var setting = {
            view: {
                selectedMulti: false,
                showIcon: false,
                addDiyDom: function (treeId, treeNode) {
                    var obj = $("#" + treeNode.tId);
                    if (treeNode.buttons) {
                        for (var i = 0; i < treeNode.buttons.length; i++) {
                            var button = treeNode.buttons[i];
                            var wrapper = $('<span class="wrap"></span>');
                            wrapper.append('<input class="cboButton" id="Button_' + button.id + '" type="checkbox"' + (button.checked ? ' checked="checked"' : '') + ' value="' + button.id + '" />');
                            wrapper.append('<label for="Button_' + button.id + '">' + button.name + '</label>');
                            obj.append(wrapper);
                        }
                    }
                }
            },
            check: {
                enable: true,
                autoCheckTrigger: true
            },
            callback: {
                onCheck: function (event, treeId, treeNode) {
                    if (treeNode.buttons != null && treeNode.buttons.length > 0) {
                        var obj = $("#" + treeNode.tId);
                        if (treeNode.checked)
                            obj.find('.cboButton').attr('checked', 'checked');
                        else {
                            obj.find('.cboButton').removeAttr('checked');
                        }
                    }
                }
            }
        };
        $(function () {
            objTree = $.fn.zTree.init($("#menuRole"), setting, nodes);
        });

        function returnParent() {
            location.href = '@Url.Action("Index")';
        }

        function save() {
            var nodes = objTree.getCheckedNodes();
            var jsonMenu = [];
            var jsonButton = [];
            for (var i = 0; i < nodes.length; i++) {
                var node = nodes[i];
                if (node.MenuID)
                    jsonMenu.push(node.MenuID);
            }

            $('.cboButton:checked').each(function (i, n) {
                var buttonV = $(n).val();
                jsonButton.push(buttonV);
            });

            Utils.ajax({
                url: '@Url.Action("SaveRole")',
                data: { 'RoleID': roleID, 'jsonMenu': jsonMenu.join(','), 'jsonButton': jsonButton.join(',') },
                type: 'POST',
                success: function (d) { if (d.Result) location.reload(); },
                error: function (status) { }
            });
        }
</script>
    <style type="text/css">
        .ztree .wrap { display: inline-block; vertical-align: middle; padding: 0 10px; }
            .ztree .wrap input[type="checkbox"] { vertical-align: middle; }
            .ztree .wrap label { vertical-align: middle; padding-left: 5px; cursor: pointer; }
    </style>
}
<div class="panel grid_8">
    <div class="panel-header">
        <span class="i-24 i-user">角色管理 -【@Model.Title】</span>
    </div>
    <div class="panel-body">
        <div class="panel-toolbar top clearfix">
            <ul>
                <li><a id="btnAdd" class="ic-16 ic-page-save" onclick="save();">保存</a></li>
                <li><a id="btnDelete" class="ic-16 ic-arrow-undo" onclick="returnParent();">返回</a></li>
            </ul>
        </div>
        <div class="pad10">
            <ul id="menuRole" class="ztree"></ul>
        </div>
    </div>
</div>